using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VKSMG
    {
        public DataRow Convert2KSMG(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryKSMG(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KSMGMANDANT"] = " ".PadRight(2 ); 
         dwReturn["KSMGJHJJ"] = " ".PadRight(0 ); 
         dwReturn["KSMGMM"] = " ".PadRight(0 ); 
         dwReturn["KSMGKSTKRS"] = " ".PadRight(2 ); 
         dwReturn["KSMGKST"] = " ".PadRight(4 ); 
         dwReturn["KSMGARTGRP"] = " ".PadRight(4 ); 
         dwReturn["KSMGAENZL"] = " ".PadRight(0 ); 
         dwReturn["KSMGAENB"] = " ".PadRight(8 ); 
         dwReturn["KSMGAEND"] = " ".PadRight(0 ); 
         dwReturn["KSMGNEUB"] = " ".PadRight(8 ); 
         dwReturn["KSMGNEUD"] = " ".PadRight(0 ); 
         dwReturn["KSMGFJ"] = " ".PadRight(5 ); 
         dwReturn["KSMGPER"] = " ".PadRight(0 ); 
         dwReturn["KSMGPKOST1"] = " ".PadRight(0 ); 
         dwReturn["KSMGPKOST2"] = " ".PadRight(0 ); 
         dwReturn["KSMGPKOST3"] = " ".PadRight(0 ); 
         dwReturn["KSMGPKOST4"] = " ".PadRight(0 ); 
         dwReturn["KSMGPKOST5"] = " ".PadRight(0 ); 
         dwReturn["KSMGSKOST1"] = " ".PadRight(0 ); 
         dwReturn["KSMGSKOST2"] = " ".PadRight(0 ); 
         dwReturn["KSMGSKOST3"] = " ".PadRight(0 ); 
         dwReturn["KSMGSKOST4"] = " ".PadRight(0 ); 
         dwReturn["KSMGSKOST5"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOSTP1"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOSTP2"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOSTP3"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOSTP4"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOSTP5"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOST1"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOST2"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOST3"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOST4"] = " ".PadRight(0 ); 
         dwReturn["KSMGIKOST5"] = " ".PadRight(0 ); 
         dwReturn["KSMGFILLER"] = " ".PadRight(1 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapKSMG(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveKSMG(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryKSMG(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapKSMG(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertKSMG(dwLocal, ITXDBVERSION);
            }
            UpdateKSMG(dwLocal);
        }
        public void InsertKSMG(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO KSMG(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryKSMG(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM KSMG WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM KSMG  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "KSMG";
            Return = Result;
            return Return;
        }
        public void DeleteKSMG(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM KSMG   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateKSMG(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE KSMG SET "
                    +"KSMGMANDANT= :KSMGMANDANT,"
                    +"KSMGJHJJ= :KSMGJHJJ,"
                    +"KSMGMM= :KSMGMM,"
                    +"KSMGKSTKRS= :KSMGKSTKRS,"
                    +"KSMGKST= :KSMGKST,"
                    +"KSMGARTGRP= :KSMGARTGRP,"
                    +"KSMGAENZL= :KSMGAENZL,"
                    +"KSMGAENB= :KSMGAENB,"
                    +"KSMGAEND= :KSMGAEND,"
                    +"KSMGNEUB= :KSMGNEUB,"
                    +"KSMGNEUD= :KSMGNEUD,"
                    +"KSMGFJ= :KSMGFJ,"
                    +"KSMGPER= :KSMGPER,"
                    +"KSMGPKOST1= :KSMGPKOST1,"
                    +"KSMGPKOST2= :KSMGPKOST2,"
                    +"KSMGPKOST3= :KSMGPKOST3,"
                    +"KSMGPKOST4= :KSMGPKOST4,"
                    +"KSMGPKOST5= :KSMGPKOST5,"
                    +"KSMGSKOST1= :KSMGSKOST1,"
                    +"KSMGSKOST2= :KSMGSKOST2,"
                    +"KSMGSKOST3= :KSMGSKOST3,"
                    +"KSMGSKOST4= :KSMGSKOST4,"
                    +"KSMGSKOST5= :KSMGSKOST5,"
                    +"KSMGIKOSTP1= :KSMGIKOSTP1,"
                    +"KSMGIKOSTP2= :KSMGIKOSTP2,"
                    +"KSMGIKOSTP3= :KSMGIKOSTP3,"
                    +"KSMGIKOSTP4= :KSMGIKOSTP4,"
                    +"KSMGIKOSTP5= :KSMGIKOSTP5,"
                    +"KSMGIKOST1= :KSMGIKOST1,"
                    +"KSMGIKOST2= :KSMGIKOST2,"
                    +"KSMGIKOST3= :KSMGIKOST3,"
                    +"KSMGIKOST4= :KSMGIKOST4,"
                    +"KSMGIKOST5= :KSMGIKOST5,"
                    +"KSMGFILLER= :KSMGFILLER,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KSMGMANDANT", OracleType.NVarChar).Value = dwData["KSMGMANDANT"].ToString(); 
          or1.Parameters.Add("KSMGJHJJ", OracleType.NVarChar).Value = dwData["KSMGJHJJ"].ToString(); 
          or1.Parameters.Add("KSMGMM", OracleType.NVarChar).Value = dwData["KSMGMM"].ToString(); 
          or1.Parameters.Add("KSMGKSTKRS", OracleType.NVarChar).Value = dwData["KSMGKSTKRS"].ToString(); 
          or1.Parameters.Add("KSMGKST", OracleType.NVarChar).Value = dwData["KSMGKST"].ToString(); 
          or1.Parameters.Add("KSMGARTGRP", OracleType.NVarChar).Value = dwData["KSMGARTGRP"].ToString(); 
          or1.Parameters.Add("KSMGAENZL", OracleType.NVarChar).Value = dwData["KSMGAENZL"].ToString(); 
          or1.Parameters.Add("KSMGAENB", OracleType.NVarChar).Value = dwData["KSMGAENB"].ToString(); 
          or1.Parameters.Add("KSMGAEND", OracleType.NVarChar).Value = dwData["KSMGAEND"].ToString(); 
          or1.Parameters.Add("KSMGNEUB", OracleType.NVarChar).Value = dwData["KSMGNEUB"].ToString(); 
          or1.Parameters.Add("KSMGNEUD", OracleType.NVarChar).Value = dwData["KSMGNEUD"].ToString(); 
          or1.Parameters.Add("KSMGFJ", OracleType.NVarChar).Value = dwData["KSMGFJ"].ToString(); 
          or1.Parameters.Add("KSMGPER", OracleType.NVarChar).Value = dwData["KSMGPER"].ToString(); 
          or1.Parameters.Add("KSMGPKOST1", OracleType.NVarChar).Value = dwData["KSMGPKOST1"].ToString(); 
          or1.Parameters.Add("KSMGPKOST2", OracleType.NVarChar).Value = dwData["KSMGPKOST2"].ToString(); 
          or1.Parameters.Add("KSMGPKOST3", OracleType.NVarChar).Value = dwData["KSMGPKOST3"].ToString(); 
          or1.Parameters.Add("KSMGPKOST4", OracleType.NVarChar).Value = dwData["KSMGPKOST4"].ToString(); 
          or1.Parameters.Add("KSMGPKOST5", OracleType.NVarChar).Value = dwData["KSMGPKOST5"].ToString(); 
          or1.Parameters.Add("KSMGSKOST1", OracleType.NVarChar).Value = dwData["KSMGSKOST1"].ToString(); 
          or1.Parameters.Add("KSMGSKOST2", OracleType.NVarChar).Value = dwData["KSMGSKOST2"].ToString(); 
          or1.Parameters.Add("KSMGSKOST3", OracleType.NVarChar).Value = dwData["KSMGSKOST3"].ToString(); 
          or1.Parameters.Add("KSMGSKOST4", OracleType.NVarChar).Value = dwData["KSMGSKOST4"].ToString(); 
          or1.Parameters.Add("KSMGSKOST5", OracleType.NVarChar).Value = dwData["KSMGSKOST5"].ToString(); 
          or1.Parameters.Add("KSMGIKOSTP1", OracleType.NVarChar).Value = dwData["KSMGIKOSTP1"].ToString(); 
          or1.Parameters.Add("KSMGIKOSTP2", OracleType.NVarChar).Value = dwData["KSMGIKOSTP2"].ToString(); 
          or1.Parameters.Add("KSMGIKOSTP3", OracleType.NVarChar).Value = dwData["KSMGIKOSTP3"].ToString(); 
          or1.Parameters.Add("KSMGIKOSTP4", OracleType.NVarChar).Value = dwData["KSMGIKOSTP4"].ToString(); 
          or1.Parameters.Add("KSMGIKOSTP5", OracleType.NVarChar).Value = dwData["KSMGIKOSTP5"].ToString(); 
          or1.Parameters.Add("KSMGIKOST1", OracleType.NVarChar).Value = dwData["KSMGIKOST1"].ToString(); 
          or1.Parameters.Add("KSMGIKOST2", OracleType.NVarChar).Value = dwData["KSMGIKOST2"].ToString(); 
          or1.Parameters.Add("KSMGIKOST3", OracleType.NVarChar).Value = dwData["KSMGIKOST3"].ToString(); 
          or1.Parameters.Add("KSMGIKOST4", OracleType.NVarChar).Value = dwData["KSMGIKOST4"].ToString(); 
          or1.Parameters.Add("KSMGIKOST5", OracleType.NVarChar).Value = dwData["KSMGIKOST5"].ToString(); 
          or1.Parameters.Add("KSMGFILLER", OracleType.NVarChar).Value = dwData["KSMGFILLER"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
